package com.timothy.android;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.timothy.android.view.BaseBean;
import com.timothy.answer.ChoiceResult;
import com.timothy.chart.BaseChart;
import com.timothy.config.Configuration;
import com.timothy.service.CommunicationService;
import com.timothy.statistics.ButtonStatistics;
import com.timothy.statistics.GradingStatistics;
import com.timothy.statistics.PriorityStatistics;
import com.timothy.statistics.StatisticsBuilder;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

@Deprecated
public class BaseAnswerActivity extends BaseProcessActivity implements OnDoubleTapListener, OnGestureListener,OnTouchListener{
	private final static String LOG_TAG = "BaseAnswerActivity";
	
	protected final static Integer QUESTION_WRAP_LENGTH_VERTIAL = 35;
	protected final static Integer QUESTION_WRAP_LENGTH_HORIZONTAL = 65;
	protected final static Integer CHOICE_NUMBER_PER_PAGE = 10;
	
	protected ButtonStatistics answer = new ButtonStatistics();
	
	//grading
	protected GradingStatistics gradingStatistics = null;
	//priority
    protected Integer priority = 1;
	protected Integer prioritySize = 0;
	protected PriorityStatistics priorityStatistics = null;
	
	protected BaseBean baseBean = new BaseBean();
	
	protected String dataXML = null;
	
	protected String displayStyle = null;
	protected String[] pagedDisplyStyles = new String[]{"1","2","3","4","5","6","9"};
	
	//answer chart list
	protected List<BaseChart> chartList = new ArrayList<BaseChart>();
	
//	protected String[] chartColorArr = new String[]{"#ff0000","#ff7f00","#ffff00","#56ff56","#0fff0f","#007fff","#7f00ff","#ff007f"};
//	protected String[] shadowColorArr = new String[]{"#bf0000","#bf5f00","#bfbf00","#5fbf00","#007f7f","#003f7f","#3f007f","#7f003f"};
	protected int textSize = 18;
	protected String questionTextColor = "#ffffff";
	protected final static int questionTextSize = 15;
	protected final static String choiceTextColor = "#000000";
	protected final static int choiceTextSize = 15;
	protected final static String answerTextColor = "#000000";
	protected final static int answerTextSize = 15;
	
	//question type
	protected String type = null;
	protected GestureDetector mGestureDetector = null;
	
	protected TextView priorityList = null;
	
	public void initialData(String dataXML,Integer pageIndex){
		Log.i(LOG_TAG,"------------start initialData-------------");
		Log.i(LOG_TAG,"parse dataXML:"+dataXML);
		
		List<String> displayStyles = new ArrayList<String>();
		Collections.addAll(displayStyles, pagedDisplyStyles);
		
		if(displayStyles.indexOf(displayStyle)!=-1){
			answer = StatisticsBuilder.parseSingleButtonAnswer(dataXML,pageIndex);
			pageNumber = StatisticsBuilder.parseSingleButtonAnswerPage(dataXML);			
		}else{
			answer = StatisticsBuilder.parseSingleButtonAnswer(dataXML);
		}
		
		Log.i(LOG_TAG,"answer message:"+answer.toMessage());
		
		List<ChoiceResult> choiceResultList = answer.getChoiceResults();
		if(choiceResultList==null||choiceResultList.size()==0){
			return;
		}
		
//		String appendTitle = "(SlideIndex:"+answer.getSlide()+")";
//		this.appendTitle(appendTitle);
//		Log.i(LOG_TAG,appendTitle);
		
		for(ChoiceResult cr:choiceResultList){
			BaseChart chart = new BaseChart();
			int index = cr.getIndex();
			Log.i(LOG_TAG,"index:"+index);
			chart.setIndex(index);
			chart.setVotes(cr.getCount());
			if(answer.getTotal()>0){
				chart.setShare(cr.getCount()*1.0/answer.getTotal());
			}else{
				chart.setShare(0);
			}
			chart.setChoiceText(String.valueOf(cr.getContent()));
			chart.setFullColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setShadowColor(Configuration.SHADOW_COLOR_ARR[index%8]);
			chart.setChoiceTextSize(textSize);
			chart.setChoiceTextColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setAnswerType(3);
			chart.setAnswerText(3);
			chart.setAnswerTextColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setAnswerTextSize(textSize);
			chartList.add(chart);
		}
		
		
		Log.i(LOG_TAG,"question name:"+answer.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(answer.getQuestion().getQuestionName());
//		baseBean.setCrList(answer.getChoiceResults());
		
		Log.i(LOG_TAG,"------------end initialData-------------");
		
		//new code
		baseBean = StatisticsBuilder.parseBaseBean(dataXML);
		
	}
	
	public void initialPriorityData(String dataXML,Integer priority,Integer pageIndex){
		Log.i(LOG_TAG,"------------start initialPriorityData-------------");
		
		List<String> displayStyles = new ArrayList<String>();
		Collections.addAll(displayStyles, pagedDisplyStyles);
		if(displayStyles.indexOf(displayStyle)!=-1){
//			answer = StatisticsBuilder.parseSingleButtonAnswer(dataXML,pageIndex);
//			pageNumber = StatisticsBuilder.parseSingleButtonAnswerPage(dataXML);	
			priorityStatistics = StatisticsBuilder.parsePriorityStatistics(dataXML,pageIndex);
		}else{
			priorityStatistics = StatisticsBuilder.parsePriorityStatistics(dataXML);
		}
		
//		statistics = StatisticsBuilder.parsePriorityStatistics(dataXML);
		baseBean.setQuestionText(priorityStatistics.getQuestion().getQuestionName());
		
		Map<Integer,List<ChoiceResult>> priorityMap = priorityStatistics.getPriorityMap();
		
		List<ChoiceResult> crList;
		if(priorityMap!=null&&priorityMap.size()>0){
			
			prioritySize = priorityMap.size();
			Log.i(LOG_TAG,"prioritySize="+prioritySize);
			
			crList = priorityMap.get(priority);
			Log.i(LOG_TAG,"priority="+priority);
			for(ChoiceResult cr:crList){
				BaseChart chart = new BaseChart();
				int index = cr.getIndex();
				Log.i(LOG_TAG,"index:"+index);
				chart.setIndex(index);
				chart.setVotes(cr.getCount());
				chart.setShare(cr.getShare());
				
				chart.setFullColor(Configuration.CHART_COLOR_ARR[index%8]);
				chart.setShadowColor(Configuration.SHADOW_COLOR_ARR[index%8]);
				
				chart.setChoiceText(cr.getContent());
				chart.setChoiceTextSize(textSize);
				chart.setChoiceTextColor(Configuration.CHART_COLOR_ARR[index%8]);
				
				chart.setAnswerType(3);
				chart.setAnswerText(3);
				chart.setAnswerTextColor(Configuration.CHART_COLOR_ARR[index%8]);
				chart.setAnswerTextSize(textSize);
				chartList.add(chart);
			}
		}
		
		Log.i(LOG_TAG,"question name:"+priorityStatistics.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(priorityStatistics.getQuestion().getQuestionName());
//		baseBean.setCrList(priorityStatistics.getPriorityMap().get(1));
		
		Log.i(LOG_TAG,"------------end initialPriorityData-------------");
	}
	
	public void initialGradingData(String dataXML){
		Log.i(LOG_TAG,"------------start initialGradingData-------------");
		Log.i(LOG_TAG,"parse dataXML:"+dataXML);
		
		gradingStatistics =  StatisticsBuilder.parseGradingStatistics(dataXML);
		Log.i(LOG_TAG,gradingStatistics.toMessage());
		
		List<ChoiceResult> choiceResultList = gradingStatistics.getChoiceResults();
		if(choiceResultList==null||choiceResultList.size()==0){
			return;
		}
		
//		String appendTitle = "(SlideIndex:"+gradingStatistics.getSlide()+")";
//		this.appendTitle(appendTitle);
//		Log.i(LOG_TAG,appendTitle);
		
		for(ChoiceResult cr:choiceResultList){
			BaseChart chart = new BaseChart();
			int index = cr.getIndex();
			Log.i(LOG_TAG,"index:"+index);
			chart.setIndex(index);
			chart.setVotes(cr.getCount());
			if(gradingStatistics.getTotal()>0){
				chart.setShare(cr.getCount()*1.0/gradingStatistics.getTotal());
			}else{
				chart.setShare(0);
			}
			chart.setChoiceText(String.valueOf(cr.getContent()));
			chart.setFullColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setShadowColor(Configuration.SHADOW_COLOR_ARR[index%8]);
			chart.setChoiceTextSize(textSize);
			chart.setChoiceTextColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setAnswerType(3);
			chart.setAnswerText(3);
			chart.setAnswerTextColor(Configuration.CHART_COLOR_ARR[index%8]);
			chart.setAnswerTextSize(textSize);
			chartList.add(chart);
		}
		
		
		Log.i(LOG_TAG,"question name:"+gradingStatistics.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(gradingStatistics.getQuestion().getQuestionName());
//		baseBean.setCrList(gradingStatistics.getChoiceResults());
		
		Log.i(LOG_TAG,"------------end initialGradingData-------------");
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG,"onCreate");
		
		Bundle bundle = this.getIntent().getExtras();
		
		//get message
		String message = bundle.getString("message");
		
		//get displayStyle
		displayStyle = bundle.getString("displayStyle");
		
		Log.i(LOG_TAG,message);
		
		//get pageIndex
		String pageIndexStr = bundle.getString("pageIndexStr");
		if(pageIndexStr!=null){
			pageIndex = Integer.valueOf(pageIndexStr);
			Log.i(LOG_TAG,"pageIndex:"+pageIndex);
		}
		
		//get question type
		type = bundle.getString("type");
		Log.i(LOG_TAG,"type:"+type);
		
		//save message steam
		dataXML = message;
		
		if("42".equals(type)){//for priority question
			String priorityStr = bundle.getString("displayPriority");
			if(priorityStr!=null&&priorityStr.length()>0){
				priority = Integer.valueOf(priorityStr);
			}else{
				priority = 1;
			}
			initialPriorityData(message,priority,pageIndex);

		}else if("45".equals(type)){//for priority question
			initialGradingData(message);
		}else{//for other question
			initialData(message,pageIndex);
		}
		
		mGestureDetector = new GestureDetector((OnGestureListener) this); 

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	protected int verticalMinDistance = 5;
	protected int horizontalMinDistance = 5;
    protected int minVelocity         = 0;

	
    protected Integer pageIndex = 1;
    protected Integer pageNumber = 1;
	
	
	//if user fling left, priority should move front,if user fling right, priority should move back
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
    	Log.i(LOG_TAG,"pageIndex:"+pageIndex);
    	Log.i(LOG_TAG,"pageNumber:"+pageNumber);
    	
    	//for vertical chart
    	if(displayStyle.equals("1")||displayStyle.equals("3")||displayStyle.equals("5")){
    		
            if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
            	Log.i(LOG_TAG,"Move Left");
            	if(type==null) type = "";
            	if(pageIndex<pageNumber)
                	processMessageWithPage(CommunicationService.MSG_SLIDE_RESULT,dataXML,++pageIndex);
            	//priority question
            	/*if("42".equals(type)&&priority>1){
            		Log.i(LOG_TAG,"priority:"+priority);
            		Log.i(LOG_TAG,"dataXML:"+dataXML);
            		processMessageForPirority(CommunicationService.MSG_SLIDE_RESULT,dataXML,--priority);
            	}*/
            } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
            	Log.i(LOG_TAG,"Move Right");
            	if(type==null) type = "";
            	if(pageIndex>1)
                	processMessageWithPage(CommunicationService.MSG_SLIDE_RESULT,dataXML,--pageIndex);
            	
            	//priority question
            	/*if("42".equals(type)&&priority<prioritySize){
            		Log.i(LOG_TAG,"priority:"+priority);
            		Log.i(LOG_TAG,"dataXML:"+dataXML);
            		processMessageForPirority(CommunicationService.MSG_SLIDE_RESULT,dataXML,++priority);
            	}*/
            }   		
    	}
    	
    	//for horizontal chart,page change
    	if(displayStyle.equals("2")||displayStyle.equals("4")||displayStyle.equals("6")||displayStyle.equals("9")){
            if (e1.getY() - e2.getY() > horizontalMinDistance && Math.abs(velocityY) > minVelocity) {
            	Log.i(LOG_TAG,"Move Left");
            	if(pageIndex<pageNumber)
                	processMessageWithPage(CommunicationService.MSG_SLIDE_RESULT,dataXML,++pageIndex);
            } else if (e2.getY() - e1.getY() > horizontalMinDistance && Math.abs(velocityY) > minVelocity) {
            	Log.i(LOG_TAG,"Move Right");
            	if(pageIndex>1)
                	processMessageWithPage(CommunicationService.MSG_SLIDE_RESULT,dataXML,--pageIndex);
            }   		
    	}

		return false;
	}


	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
